<!doctype html>
<head>
<title>Test Clear-Site-Data and partitioned cookies</title>
<script src="/resources/testharness.js"></script>
<script src="/cookies/resources/testharness-helpers.js"></script>
</head>
<body>
<script>

const step = new URL(location.href).searchParams.get("step");
const originFromHost =
    host => `https://${host}:${window.location.port}`;

promise_test(async () => {
  assert_equals(window.location.hostname, TEST_HOST);
  assert_true(step == 1 || step == 2);

  if (step == 1) {
    assert_equals(document.cookie, "");

    const cookieLine = "__Host-pc=0; Secure; Path=/; SameSite=None; Partitioned";

    document.cookie = cookieLine;
    assert_true(document.cookie.includes("__Host-pc"));

    // Test that Clear-Site-Data deletes the cookie in this partition.
    const resp = await fetch(
        "/cookies/resources/clear-site-data.php", {method: "GET"});
    assert_equals(resp.status, 200);
    assert_equals(document.cookie, "");

    // Reset the cookie, we will use this later to verify that this cookie
    // should not get removed when another partition receives Clear-Site-Data.
    document.cookie = cookieLine;
  }

  assert_true(document.cookie.includes("__Host-pc"));

  if (step == 1) {
    const url = new URL(
        "/cookies/partitioned-cookies/resources/" +
            "clear-site-data-cross-site-window.html",
        originFromHost(TEST_HOST));
    const popup = window.open(String(url));
    fetch_tests_from_window(popup);
  }
}, (step == 1 ? "Clearing partitioned cookies"
              : "Clear-Site-Data does not leak across partitions"));

</script>
</body>